package cn.tedu.javagame.FirePlane;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Heart extends FlyObject {
    static Image heartImg;
    static List<Image> heartImgs = new ArrayList<>();
    int type;//0加生命 1加火力  2 加金币...
    int state;//0往左移动 1往右移动
    static {
        heartImg = new ImageIcon("plane/qq00.png").getImage();
        for(int i = 1; i < 9; i++){
            // 加载图片
            heartImgs.add(new ImageIcon("plane/qq0" + i + ".png").getImage());
        }
        img = heartImg;
    }

    public Heart() {
        this.x = (int)(Math.random()*(ShootFrame.WIDTH-img.getWidth(null)));
        this.y = -img.getHeight(null);
        this.type = (int)(Math.random()*3);
        this.state = (int)(Math.random()*2);
    }

    @Override
    public void move() {
        this.y+=speed;
        if(this.state==0){
            this.x-=5;
        }
        if(this.state==1){
            this.x+=5;
        }
        //碰到边界的时候
        if(this.x<=0){this.state=1;}
        if(this.x+this.img.getWidth(null)>=ShootFrame.WIDTH){this.state=0;}
    }

    @Override
    public void step() {
        // 添加动画效果
        int index = (int) (System.currentTimeMillis() / 100 % heartImgs.size());
        img = heartImgs.get(index);
    }

    public boolean hitHero(Hero h) {
        Rectangle heroRect = new Rectangle(h.x, h.y, h.heroImg.getWidth(null), h.heroImg.getHeight(null));
        Rectangle heartRect = new Rectangle(x, y, img.getWidth(null), img.getHeight(null));
        return heroRect.intersects(heartRect);
    }

    public void increaseHeroLife(Hero h) {
        h.life += 20; // 每次碰到红心增加20点生命值
        if (h.life > 100) {
            h.life = 100; // 生命值上限为100
        }
    }
}